#include "AdminFoodOrderListWindow.h"
#include "ui_AdminFoodOrderListWindow.h"

AdminFoodOrderListWindow::AdminFoodOrderListWindow(QWidget *parent) :
    QListWidget(parent),
    ui(new Ui::AdminFoodOrderListWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("订单列表");
    /**
      * @leiteorz
      * 加载订单列表
      */
    FoodOrderTable food_order_table;

    FoodOrder *foodOrder = new FoodOrder[100];
    foodOrder = food_order_table.queryAllFoodOrder();   //所有订单

    int i = 0;

    while (foodOrder[i].bill != "") {
       /**
         * @leiteorz
         * 绘制ListItem
         */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(789,89));
        OrderItem *orderItem = new OrderItem();
        orderItem->init(foodOrder[i].id);
        this->setItemWidget(item,orderItem);

        connect(orderItem,SIGNAL(queryOrder(FoodOrder)),this,SLOT(replyQueryOrder(FoodOrder)));

        i++;
    }
}

AdminFoodOrderListWindow::~AdminFoodOrderListWindow()
{
    delete ui;
}

//显示订单列表界面
void AdminFoodOrderListWindow::replyShowAdminFoodOrderListWindow(){
    this->show();
}

//回应查询按钮
void AdminFoodOrderListWindow::replyQueryOrder(FoodOrder foodOrder){
    OrderWindow *orderWindow = new OrderWindow();
    orderWindow->init(foodOrder);
    orderWindow->show();
    connect(orderWindow,SIGNAL(dataChange()),this,SLOT(replyDataChange()));
}

//数据改变
void AdminFoodOrderListWindow::replyDataChange(){
    this->clear();

    /**
      * @leiteorz
      * 加载订单列表
      */
    FoodOrderTable food_order_table;

    FoodOrder *foodOrder = new FoodOrder[100];
    foodOrder = food_order_table.queryAllFoodOrder();   //所有订单

    int i = 0;

    while (foodOrder[i].bill != "") {
       /**
         * @leiteorz
         * 绘制ListItem
         */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(789,89));
        OrderItem *orderItem = new OrderItem();
        orderItem->init(foodOrder[i].id);
        this->setItemWidget(item,orderItem);

        connect(orderItem,SIGNAL(queryOrder(FoodOrder)),this,SLOT(replyQueryOrder(FoodOrder)));

        i++;
    }
}

void AdminFoodOrderListWindow::on_freshen_btn_clicked(){
    this->clear();

    /**
      * @leiteorz
      * 加载订单列表
      */
    FoodOrderTable food_order_table;

    FoodOrder *foodOrder = new FoodOrder[100];
    foodOrder = food_order_table.queryAllFoodOrder();   //所有订单

    int i = 0;

    while (foodOrder[i].bill != "") {
       /**
         * @leiteorz
         * 绘制ListItem
         */
        QListWidgetItem *item = new QListWidgetItem(this);
        item->setSizeHint(QSize(789,89));
        OrderItem *orderItem = new OrderItem();
        orderItem->init(foodOrder[i].id);
        this->setItemWidget(item,orderItem);

        connect(orderItem,SIGNAL(queryOrder(FoodOrder)),this,SLOT(replyQueryOrder(FoodOrder)));

        i++;
    }
}
